<?php
class classUsersService{
    public function bIsLogged(){
        $_user_sess = new classSessions();
        $user_sess_value = $_user_sess->Open(_SESS_USER_);
        
        if(is_numeric($user_sess_value) and $user_sess_value>0){        
            $sql_result = mysql_query('SELECT COUNT(*) FROM users WHERE ID=\''.$user_sess_value.'\';');
            $sql_row    = mysql_fetch_row($sql_result);    
            if ($sql_row[0]>0)
              return true;
            else
              return false;
        }
        else return false;
    }
    
    public function iGetIdOfLoggedUser(){
        if($this->bIsLogged()){
            $_user_sess = new classSessions();
            $user_sess_value = $_user_sess->Open(_SESS_USER_);

            if(is_numeric($user_sess_value) and $user_sess_value>0){        
                $sql_result = mysql_query('SELECT COUNT(*) FROM users WHERE ID=\''.$user_sess_value.'\';');
                $sql_row    = mysql_fetch_row($sql_result);    
                if ($sql_row[0]>0)
                  return $user_sess_value;
                else
                  return 0;
            }
            else return 0;
        }
    }
    
    public function getUserInfo(){
        $_user_sess = new classSessions();
        $user_sess_value = $_user_sess->Open(_SESS_USER_);
        if(trim($user_sess_value)=='' or $user_sess_value==0 or !is_numeric($user_sess_value))
          $user_sess_value=0;
    
        $sql_result	= mysql_query('SELECT * FROM users WHERE ID=\''.$user_sess_value.'\';');
        $sql_object	= mysql_fetch_object($sql_result);
        $result['ID']             = $sql_object->ID;
        $result['U_Name']         = $sql_object->U_Name;
        $result['U_Surname']      = $sql_object->U_Surname;
        $result['U_City']         = $sql_object->U_City;
        $result['U_Street']       = $sql_object->U_Street;
        $result['U_PostID']       = $sql_object->U_PostID;
        $result['U_Phone']        = $sql_object->U_Phone;
        $result['U_Mobile']       = $sql_object->U_Mobile;
        $result['U_Email']        = $sql_object->U_Email;
        $result['U_BirthDate']    = $sql_object->U_BirthDate;
        $result['U_Organisation'] = $sql_object->U_Organisation;
        $result['U_ICO']          = $sql_object->U_ICO;
        $result['U_DIC']          = $sql_object->U_DIC;
        
        return $result;
    }
    
    public function getUserDiscountPermission($iUID){
        if(trim($iUID)=='' or !is_numeric($iUID) or $iUID<1) return false;
        $sql_result = mysql_query('
            SELECT ID_user
            FROM discount_automatic
            WHERE 
                ID_user='.$iUID.' AND 
                (
                    Price_automatic <= (SELECT Top_price FROM top_user WHERE ID_top='.$iUID.') OR
                    NOT EXISTS (SELECT Top_price FROM top_user WHERE ID_top='.$iUID.')
                ) AND 
                EXISTS (SELECT ID_user FROM discount WHERE ID_user='.$iUID.' AND discount_value>0)
        ');
        $sql_object = mysql_fetch_object($sql_result);
        if($iUID == $sql_object->ID_user) return true; else return false;
    }

    public function bIsSetUnloggedUser(){
        $_user_sess = new classSessions();
        $user_sess_value = $_user_sess->Open(_SESS_USER_DATA_);

        if(trim($user_sess_value)!='') return true; else return false;
    }

    public function getUnloggedUserInfo(){
        if(!$this->bIsSetUnloggedUser()) return '';

        $_user_sess = new classSessions();
        $user_sess_value = $_user_sess->Open(_SESS_USER_DATA_);

        $user_data_arr = explode(_USER_TEMP_DATA_ITEM_DELIMITER_, $user_sess_value);
        foreach ($user_data_arr as $uarr){
            $uitem = explode(_USER_TEMP_DATA_INDEX_VALUE_DELIMITER_, $uarr);
            $result[trim($uitem[0])] = trim($uitem[1]);
        }

        return $result;
    }
}
?>
